//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "YoYInflationFloor.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/CashFlow.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/YoYInflationTermStructure.h>
#include <gen/QL/Cashflows/YoYInflationCoupon.h>
#include <gen/QL/Instruments/YoYInflationCapFloor.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Cashflows;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CYoYInflationFloor::CYoYInflationFloor (Cephei::IVector<Cephei::QL::ICashFlow^>^ yoyLeg, Cephei::IVector<Double>^ exerciseRates, Cephei::QL::IPricingEngine^ QL_Pricer) : CYoYInflationCapFloor(CYoYInflationFloor::typeid)
{
    CoVector<Cephei::QL::ICashFlow^>^ _CyoyLeg;
    CoVector<Double>^ _CexerciseRates;
    try
    {
#ifdef HANDLE
        _phYoYInflationFloor = NULL;
#endif
        _CyoyLeg = safe_cast<CoVector<Cephei::QL::ICashFlow^>^> (yoyLeg);
        _CyoyLeg ->Lock ();
        INativeVector<Cephei::QL::ICashFlow^>^ _NCIyoyLeg = _CyoyLeg->getFeature (NativeFeature::shared_ptr);
        CCashFlowVector^ _NCyoyLeg = safe_cast<CCashFlowVector^>(_NCIyoyLeg);
        std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _yoyLeg = static_cast<std::vector<boost::shared_ptr<QuantLib::CashFlow> >&> (_NCyoyLeg->GetShared ());
        CoVector<Double>^ _CexerciseRates = safe_cast<CoVector<Double>^> (exerciseRates);
        _CexerciseRates->Lock();
        INativeVector<Double>^ _NCIexerciseRates = _CexerciseRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCexerciseRates = safe_cast<CDoubleVector^>(_NCIexerciseRates);
        std::vector<QuantLib::Rate>& _exerciseRates = static_cast<std::vector<QuantLib::Rate>&> (_NCexerciseRates->GetReference ());
        _ppYoYInflationFloor = new boost::shared_ptr<QuantLib::YoYInflationFloor> (new QuantLib::YoYInflationFloor ( _yoyLeg,  _exerciseRates ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppYoYInflationFloor)->setPricingEngine (_QL_Pricer);
        SetYoYInflationCapFloor (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationFloor));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CyoyLeg != nullptr) _CyoyLeg->Unlock();
        if (_CexerciseRates != nullptr) _CexerciseRates->Unlock();
    }
}
Cephei::QL::Instruments::CYoYInflationFloor::CYoYInflationFloor (boost::shared_ptr<QuantLib::YoYInflationFloor>& childNative, Object^ owner) : CYoYInflationCapFloor(CYoYInflationFloor::typeid)
{
#ifdef HANDLE
	_phYoYInflationFloor = NULL;
#endif
	_ppYoYInflationFloor = &childNative;
    _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationFloor));
}
Cephei::QL::Instruments::CYoYInflationFloor::CYoYInflationFloor (QuantLib::YoYInflationFloor& childNative, Object^ owner) : CYoYInflationCapFloor(CYoYInflationFloor::typeid)
{
#ifdef HANDLE
	_phYoYInflationFloor = NULL;
#endif
	_ppYoYInflationFloor = new boost::shared_ptr<QuantLib::YoYInflationFloor> (&childNative);
    _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationFloor));
    _YoYInflationFloorOwner = owner;
    _YoYInflationCapFloorOwner = owner;
}

Cephei::QL::Instruments::CYoYInflationFloor::CYoYInflationFloor (CYoYInflationFloor^ copy) : CYoYInflationCapFloor(CYoYInflationFloor::typeid)
{
#ifdef HANDLE
	_phYoYInflationFloor = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYInflationFloor = new boost::shared_ptr<QuantLib::YoYInflationFloor> (copy->GetShared());
        _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationFloor));
    }
}
Cephei::QL::Instruments::CYoYInflationFloor::CYoYInflationFloor (System::Type^ t) : CYoYInflationCapFloor(CYoYInflationFloor::typeid)
{
#ifdef HANDLE
	_phYoYInflationFloor = NULL;
#endif
	if (!t->IsSubclassOf(CYoYInflationFloor::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CYoYInflationFloor::CYoYInflationFloor (QuantLib::Handle<QuantLib::YoYInflationFloor>& childNative, Object^ owner)  : CYoYInflationCapFloor(CYoYInflationFloor::typeid)
{
	_phYoYInflationFloor = &childNative;
	_ppYoYInflationFloor = &static_cast<boost::shared_ptr<QuantLib::YoYInflationFloor>>(childNative.currentLink());
    _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationFloor));
    _YoYInflationFloorOwner = owner;
}
Cephei::QL::Instruments::CYoYInflationFloor::CYoYInflationFloor (QuantLib::Handle<QuantLib::YoYInflationFloor> childNative)  : CYoYInflationCapFloor(CYoYInflationFloor::typeid)
{
	_phYoYInflationFloor = &childNative;
	_ppYoYInflationFloor = &static_cast<boost::shared_ptr<QuantLib::YoYInflationFloor>>(childNative.currentLink());
    _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationFloor));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CYoYInflationFloor::CYoYInflationFloor (QuantLib::YoYInflationFloor childNative)  : CYoYInflationCapFloor(CYoYInflationFloor::typeid)
{
#ifdef HANDLE
	_phYoYInflationFloor = NULL;
#endif
	_ppYoYInflationFloor = new boost::shared_ptr<QuantLib::YoYInflationFloor> (new QuantLib::YoYInflationFloor (childNative));
    _ppYoYInflationCapFloor = new boost::shared_ptr<QuantLib::YoYInflationCapFloor> (boost::dynamic_pointer_cast<QuantLib::YoYInflationCapFloor> (*_ppYoYInflationFloor));
}
#endif

Cephei::QL::Instruments::CYoYInflationFloor::~CYoYInflationFloor ()
{
    if (_ppYoYInflationFloor != NULL)
    {
	    delete _ppYoYInflationFloor;
        _ppYoYInflationFloor = NULL;
    }
}
Cephei::QL::Instruments::CYoYInflationFloor::!CYoYInflationFloor ()
{
    if (_ppYoYInflationFloor != NULL)
    {
	    delete _ppYoYInflationFloor;
    }
}
QuantLib::YoYInflationFloor& Cephei::QL::Instruments::CYoYInflationFloor::GetReference ()
{
    if (_ppYoYInflationFloor == NULL) throw gcnew NativeNullException ();
	return **_ppYoYInflationFloor;
}
boost::shared_ptr<QuantLib::YoYInflationFloor>& Cephei::QL::Instruments::CYoYInflationFloor::GetShared ()
{
    if (_ppYoYInflationFloor == NULL) throw gcnew NativeNullException ();
	return *_ppYoYInflationFloor;
}
QuantLib::YoYInflationFloor* Cephei::QL::Instruments::CYoYInflationFloor::GetPointer ()
{
    if (_ppYoYInflationFloor == NULL) throw gcnew NativeNullException ();
	return &**_ppYoYInflationFloor;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYInflationFloor>& Cephei::QL::Instruments::CYoYInflationFloor::GetHandle ()
{
	if (_phYoYInflationFloor == NULL)
	{
		_phYoYInflationFloor = new Handle<QuantLib::YoYInflationFloor> (*_ppYoYInflationFloor);
	}
	return *_phYoYInflationFloor;
}
#endif
bool Cephei::QL::Instruments::CYoYInflationFloor::HasNative () 
{
	return (_ppYoYInflationFloor != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IYoYInflationFloor^ Cephei::QL::Instruments::CYoYInflationFloor_Factory::Create (Cephei::IVector<Cephei::QL::ICashFlow^>^ yoyLeg, Cephei::IVector<Double>^ exerciseRates, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CYoYInflationFloor ( yoyLeg,  exerciseRates,  QL_Pricer);
}
